﻿using System.IO;
using System;
using System.Threading.Tasks;
using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.HR.Dto;
using NP.BPMReportPlatform.Leave;
using NP.BPMReportPlatform.Leave.Dto;
using NP.BPMReportPlatform.UserTrial.Dto;
using Volo.Abp.Application.Dtos;
using NP.BPMReportPlatform.Entities.BPM_Business;
using NP.BPMReportPlatform.MaterialMasterData.Dto;
using System.Collections.Generic;
using static NP.BPMReportPlatform.Leave.Dto.SpecialApproverDto;
using NP.BPMReportPlatform.RecurringJobs;

namespace NP.BPMReportPlatform.UserTrial
{
    /// <summary>
    /// 请休假接口
    /// </summary>
    //[Authorize]
    [ApiController]
    [Route("api/user-leave")]
    public class LeaveController : BPMReportPlatformController
    {
        private readonly ILeaveService _leaveService;
        private readonly ILeaveSyncJob _leaveSyncJob;

        public LeaveController(ILeaveService leaveService, ILeaveSyncJob leaveSyncJob)
        {
            _leaveService = leaveService;
            _leaveSyncJob = leaveSyncJob;
        }

        /// <summary>
        /// 请休假数据查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetLeaveResultAsync")]
        public async Task<PagedResultDto<GetLeaveResultOutput>> GetAllAsync([FromBody] GetLeaveResultInput input)
        {
            return await _leaveService.GetLeaveResult(input);
        }

        /// <summary>
        /// 请休假数据导出
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportLeaveResultAsync")]
        public async Task<FileResult> ExportLeaveResultAsync([FromBody] GetLeaveResultInput input)
        {
            var rst = await _leaveService.ExportLeaveResultAsync(input);
            string downloadFileName = System.Guid.NewGuid().ToString("N") + ".xlsx";
            rst.Close();
            return File(rst.ToArray(), "application/octet-stream", $"{downloadFileName}.xlsx");
        }

        /// <summary>
        /// SAP数据推送
        /// </summary>
        /// <param name="instanceNumber"></param>
        /// <param name="processType"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PushSAPAsync")]
        public async Task<string> PushSAPAsync(string instanceNumber, string processType)
        {
            return await _leaveService.PushSAPAsync(instanceNumber, processType);

        }

        #region 特殊审批流配置

        /// <summary>
        /// 特殊审批人信息查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetSpecialApproverAsync")]
        public async Task<PagedResultDto<LeaveSpecialApproverEntity>> GetSpecialApproverAsync([FromBody] GetSpecialLeaveResultInput input)
        {
            return await _leaveService.GetSpecialApproverAsync(input);
        }

        /// <summary>
        /// 审批信息导出
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("SpecialApproverExportAsync")]
        public async Task<XlsxFileResult<ExportOutput>> SpecialApproverExportAsync([FromBody] GetSpecialLeaveResultInput input)
        {
            input.PageSize = 999;
            var rst = (await _leaveService.GetSpecialApproverAsync(input)).Items;
            var result = new List<ExportOutput>();
            return new XlsxFileResult<ExportOutput>(ObjectMapper.Map(rst, result), "特殊审批人导出");
        }

        /// <summary>
        /// 审批信息导入
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ImporterExcelAsync")]
        public async Task<ImportResultExt<SpecialApproverDto.ImportInput>> ImporterExcelAsync(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<SpecialApproverDto.ImportInput>();
                res.Exception = new Exception("请上传文件！");
                return res;
            }
            var s = file.OpenReadStream();
            var bytes = StreamToBytes(s);
            return await _leaveService.ImporterExcelAsync(bytes);
        }

        /// <summary>
        /// 保存审批配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("EditAsync")]
        public async Task EditAsync([FromBody] LeaveSpecialApproverEntity input)
        {
            await _leaveService.EditAsync(input);
        }

        /// <summary>
        /// 删除审批配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("DeleteAsync")]
        public async Task DeleteAsync([FromQuery] string id)
        {
            await _leaveService.DeleteAsync(id);
        }

        #endregion

        private byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        [HttpPost("DoSyncWork")]
        public async Task DoSyncWork()
        {
            await _leaveSyncJob.DoSyncWork();
        }

        [HttpPost("DoSnendEmailWork")]
        public async Task DoSnendEmailWork()
        {
            await _leaveSyncJob.DoSnendEmailWork();
        }
    }
}